package com.funtl.framework.web.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 权限拦截器
 * @author wmli
 *
 */
public class PurviewInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 373171613093060713L;

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		boolean hasPurview = false;
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
		if (!request.getRequestURI().endsWith("login") 
				&& !request.getRequestURI().endsWith("loginOut")
				&& !request.getRequestURI().endsWith("admin/main")
				&& !request.getRequestURI().endsWith("admin/purview")) {
			List<String> actionMenus = (List<String>) request.getSession().getAttribute("actionMenus");
			if (actionMenus != null && actionMenus.size() > 0) {
				for (String actionMenu : actionMenus) {
					if (request.getRequestURI().endsWith(actionMenu)) {
						hasPurview = true;
						break;
					}
				}
			}
			
			if (!hasPurview) {
				response.sendRedirect(request.getContextPath() + "/system/admin/purview");
				return null;
			}
		}
		return invocation.invoke();
	}

}
